package com.ruoyi.type.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.type.domain.Caricature;
import com.ruoyi.type.service.ICaricatureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 漫画Controller
 * 
 * @author ucu
 * @date 2024-05-04
 */
@RestController
@RequestMapping("/type/caricature")
public class CaricatureController extends BaseController
{
    @Autowired
    private ICaricatureService caricatureService;

    @Autowired
    private ISysConfigService configService;
    /**
     * 查询漫画列表
     */
    @PreAuthorize("@ss.hasPermi('type:caricature:list')")
    @GetMapping("/list")
    public TableDataInfo list(Caricature caricature)
    {
        startPage();
        List<Caricature> list = caricatureService.selectCaricatureList(caricature);
        return getDataTable(list);
    }

    /**
     * 导出漫画列表
     */
    @PreAuthorize("@ss.hasPermi('type:caricature:export')")
    @Log(title = "漫画", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Caricature caricature)
    {
        List<Caricature> list = caricatureService.selectCaricatureList(caricature);
        ExcelUtil<Caricature> util = new ExcelUtil<Caricature>(Caricature.class);
        util.exportExcel(response, list, "漫画数据");
    }

    /**
     * 获取漫画详细信息
     */
    @PreAuthorize("@ss.hasPermi('type:caricature:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(caricatureService.selectCaricatureById(id));
    }

    /**
     * 新增漫画
     */
    @PreAuthorize("@ss.hasPermi('type:caricature:add')")
    @Log(title = "漫画", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Caricature caricature)
    {
        if (!("true".equals(configService.selectConfigByKey("info.image.upload")))) {
            caricature.setPicture(null);
        }
        if (!("true".equals(configService.selectConfigByKey("info.image.url")))) {
            caricature.setPicture(null);
        }
        caricature.setReTime(new Date());
        return toAjax(caricatureService.insertCaricature(caricature));
    }

    /**
     * 修改漫画
     */
    @PreAuthorize("@ss.hasPermi('type:caricature:edit')")
    @Log(title = "漫画", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Caricature caricature)
    {
        if (!("true".equals(configService.selectConfigByKey("info.image.upload")))) {
            caricature.setPicture(null);
        }
        if (!("true".equals(configService.selectConfigByKey("info.image.url")))) {
            caricature.setPicture(null);
        }
        caricature.setReTime(new Date());
        return toAjax(caricatureService.updateCaricature(caricature));
    }

    /**
     * 删除漫画
     */
    @PreAuthorize("@ss.hasPermi('type:caricature:remove')")
    @Log(title = "漫画", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(caricatureService.deleteCaricatureByIds(ids));
    }
}
